package com.hunt.service.impl;

import com.hunt.dao.UserInfoMapper;
import com.hunt.model.dto.LoginInfo_new;
import com.hunt.model.dto.UserLevel;
import com.hunt.model.entity.UserInfo;
import com.hunt.service.ManagementService;
import com.hunt.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author sjs
 * @className
 * @description
 * @date 2020/10/22
 */
@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ManagementService managementService;

    @Override
    public UserInfo findUserInfoByUserId(String userId) {
        return userInfoMapper.findUserInfoByUserId(userId);
    }

    @Override
    public UserInfo findUserInfoByPhone(String phone) {
        return userInfoMapper.findUserInfoByPhone(phone);
    }



    @Override
    public UserInfo userInfoEncrypt(String code) {
        UserInfo infoByUserId=null;
        //如果是有效手机号 就先查询是否是已经注册的手机号
        if (isValidPhoneNumber(code)) {
            int countphone=userInfoMapper.selectCountByPhone(code);
            if(countphone==1){
                infoByUserId = findUserInfoByPhone(code);
            }
            if(countphone>1){
                infoByUserId = userInfoMapper.findUserInfoByPhoneAndId(code);
                return infoByUserId;
            }
        }
        //如果手机号查不到那就查id号
        if(infoByUserId == null) {
            infoByUserId = findUserInfoByUserId(code);
        }
        //如果手机号为空 生成一个用户
        if(infoByUserId == null) {
            //获取用户级别对应到期时间
            UserLevel userLevel = managementService.getUserLevelByid("0");
            int mDays = userLevel.getEtime();
            //计算开户时间和到期时间
            SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
            String khTime = f.format(new Date());//开户日期


            Calendar ca = Calendar.getInstance();
            ca.add(Calendar.DATE, mDays);// num为增加的天数，可以改变的
            Date mDate = new Date();
            mDate = ca.getTime();
            String enddate = f.format(mDate);


            LoginInfo_new mUserInfo = new LoginInfo_new();
            mUserInfo.setUserid(code);
            mUserInfo.setUsername("试用账户");
            mUserInfo.setPwd(code.substring(code.length()-6));
            mUserInfo.setGroupname("1");
            mUserInfo.setArea("10");
            mUserInfo.setNotlogin(2);
            mUserInfo.setValiddate(enddate);//到期日
            mUserInfo.setViewnews("1");
            mUserInfo.setViewgp(2);
            mUserInfo.setViewqh(10);
            mUserInfo.setViewwp(10);
            mUserInfo.setViewwh(10);
            mUserInfo.setViewgg(10);
            mUserInfo.setUserlevel("0");
            mUserInfo.setMemo_text("10");
            mUserInfo.setViewggmax(0);
            mUserInfo.setViewqhmax(0);
            mUserInfo.setViewwpmax(0);
            mUserInfo.setViewwhmax(0);
            mUserInfo.setViewggmax(0);
            mUserInfo.setUsercfg("|express=mf.fun");
            mUserInfo.setBegintime(1);
            mUserInfo.setEndtime(2);
            mUserInfo.setUsedtotaltime(1.0f);
            mUserInfo.setMax_login_day("3");
            mUserInfo.setKaihutime(khTime);
            mUserInfo.setPhone(code);

            long id = managementService.insertAccountChannel2(mUserInfo);
           return findUserInfoByUserId(code);
       }
        return  infoByUserId;
    }
    public  boolean isValidPhoneNumber(String phoneNumber) {
        // 定义手机号的正则表达式
        String regex = "^1[3-9]\\d{9}$";

        // 编译正则表达式
        Pattern pattern = Pattern.compile(regex);

        // 创建匹配器
        Matcher matcher = pattern.matcher(phoneNumber);

        // 判断是否匹配
        return matcher.matches();
    }

}
